// juanti.ts
import request from "@/utils/request";
import axios from "axios";

// 定义通用 API 响应类型
export interface ApiResponse<T> {
  gradeLevel: number | null;
  title: string;
  analyze: string;
  score(score: any): number;
  difficult: number;
  items: boolean;
  questionType: number;
  correct: string;
  id: number | null;
  name: string;
  level: number | null;
  subjectId: string;
  paperType: number;
  suggestTime: any;
  limitDateTime: boolean;
  titleItems: any;
  list: never[];
  total: number;
  code: number;
  message: string;
  response: T;
}

// 学科类型定义
export interface Subject {
  id: number;
  name: string;
  level: number;
  levelName: string;
}

// 学科详情类型
export interface SubjectDetail {
  id: number;
  name: string;
  level: number;
  levelName: string;
}

// 试卷分页请求参数类型
export type ExamPaperPageParams = {
  id?: string | null;
  level?: number | null;
  subjectId?: number | null;
  pageIndex: number;
  pageSize: number;
};

// 试卷项类型
export type ExamPaperItem = {
  id: number;
  name: string;
  questionCount: number;
  score: number;
  createTime: string;
  createUser: number;
  subjectId: number;
  paperType: number;
  frameTextContentId: number;
};

// 试卷分页响应类型
export type ExamPaperPageResponse = {
  total: number;
  list: ExamPaperItem[];
};

// 试卷详情类型定义
export interface ExamPaperDetail {
  id: number;
  level: number;
  subjectId: number;
  paperType: number;
  name: string;
  suggestTime: number;
  limitDateTime: [string, string] | null;
  titleItems: Array<{
    name: string;
    questionItems: Array<{
      id: number;
      questionType: number;
      subjectId: number;
      title: string;
      gradeLevel: number;
      items: Array<{
        prefix: string;
        content: string;
        score: number | null;
        itemUuid: string | null;
      }>;
      analyze: string;
      correctArray: string[] | null;
      correct: string;
      score: string;
      difficult: number;
      itemOrder: number;
    }>;
  }>;
  score: string;
}

// 题目分页请求参数类型
export type QuestionPageParams = {
  id?: number | null;
  title?: string | null; // 添加 title 参数
  questionType?: number | null;
  level?: number | null;
  subjectId?: number | null;
  pageIndex: number;
  pageSize: number;
};

// 题目列表项类型
export type QuestionItem = {
  id: number;
  questionType: number;
  textContentId: number | null;
  createTime: string;
  subjectId: number;
  createUser: number;
  score: string;
  status: number;
  correct: string;
  analyzeTextContentId: number | null;
  difficult: number;
  shortTitle: string;
  title: string; // 添加标题字段
};

// 题目分页响应类型
export type QuestionPageResponse = {
  total: number;
  list: QuestionItem[];
};

// 题目新增或编辑请求参数类型
export type QuestionEditParams = {
  id: number;
  questionType: number;
  subjectId: number;
  title: string;
  gradeLevel: number;
  items: any[];
  analyze: string;
  correctArray: string[] | null;
  correct: string;
  score: number;
  difficult: number;
  itemOrder: number | null;
};

// ========== API 接口函数 ========== //

// 获取学科详情接口
export const getSubjectDetail = async (
  gradeLevel: number
): Promise<ApiResponse<SubjectDetail>> => {
  try {
    const response = await axios.post(
      `/api/admin/education/subject/select/${gradeLevel}`
    );
    return response.data;
  } catch (error) {
    console.error("获取学科详情失败:", error);
    throw error;
  }
};

// 获取学科列表接口
export function getSubjectList() {
  return request.post<ApiResponse<Subject[]>>(
    "/api/admin/education/subject/list"
  );
}

// 试卷分页接口
export function getExamPaperPage(data: ExamPaperPageParams) {
  return request.post<ApiResponse<ExamPaperPageResponse>>(
    "/api/admin/exam/paper/page",
    data
  );
}

// 删除试卷接口
export function examPaperDelete(id: number) {
  return request.post<ApiResponse<null>>(
    `/api/admin/exam/paper/delete/${id}`
  );
}

// 题目分页查询接口
export function getQuestionPage(data: QuestionPageParams) {
  return request.post<ApiResponse<QuestionPageResponse>>(
    "/api/admin/question/page",
    data
  );
}

// 试卷详情接口
export const getExamPaperDetail = (id: number) => {
  return request.post<ApiResponse<ExamPaperDetail>>(
    `/api/admin/exam/paper/select/${id}`
  );
};

// 编辑试卷接口
export const editExamPaper = (data: any) => {
  return request.post<ApiResponse<null>>(
    "/api/admin/exam/paper/edit",
    data
  );
};

// 题目删除接口
export function questionDelete(id: number) {
  return request.post<ApiResponse<null>>(
    `/api/admin/question/delete/${id}`
  );
}

// 题目新增或编辑接口
export function editQuestion(data: QuestionEditParams) {
  return request.post<ApiResponse<null>>(
    "/api/admin/question/edit",
    data
  );
}

export const getQuestionDetail = (id: number) => {
  return request.post<ApiResponse<QuestionEditParams>>(
    `/api/admin/question/select/${id}`
  );
};
